package com.cy.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.dao.BackPurviewDao;
import com.cy.entity.BackPurview;
import com.cy.entity.NormalStatus;
import com.cy.service.BackPurviewService;

@Service("backPurviewService")
public class BackPurviewServiceImpl implements BackPurviewService<BackPurview>{

	private BackPurviewDao backPurviewDao;
	
	public String saveBackPurviewDWR(BackPurview purview, Long parentId) {
		String formatSortNo=String.format("%06d",purview.getSortNo());
		if(parentId==null){
			purview.setParent(null);
			purview.setSortTree(formatSortNo);
		}else{
			BackPurview parenetBP=backPurviewDao.getObject(BackPurview.class, parentId);
			purview.setParent(parenetBP);
			purview.setSortTree(parenetBP.getSortTree()+"-"+formatSortNo);
		}
		purview.setStatus(NormalStatus.NORMAL);
		if(backPurviewDao.listBackPurview(purview.getSortTree(), purview.getLevel()).size()>0){
			return "对不起,你所输入的序号在该等级下已存在";
		}else{
			backPurviewDao.saveBackPurview(purview);
		}
		return null;
	}

	public BackPurview eidtBackPurviewDWR(BackPurview purview) {
		BackPurview backPurview=backPurviewDao.getObject(BackPurview.class, purview.getId());
		backPurview.setName(purview.getName());
		String formatSortNo=String.format("%06d",purview.getSortNo());
		if(backPurview.getParent()==null){
			backPurview.setSortTree(formatSortNo);
		}else{
			String oldTree=backPurview.getSortTree().split("-")[0];
			backPurview.setSortTree(oldTree+"-"+formatSortNo);
		}
		backPurview.setSortNo(purview.getSortNo());
		backPurview.setActionName(purview.getActionName());
		return backPurview;
	}

	public void delBackPurviewDWR(Long id, boolean isParent) {
		backPurviewDao.delBackPurviewDWR(id, isParent);
		
	}
	public List<BackPurview> listBackPurviewByParent(Long parentId) {
		return backPurviewDao.listBackPurviewByParent(parentId);
	}
	
	public List<BackPurview> listBackPurviewByLevel(Integer level) {
		return backPurviewDao.listBackPurviewByLevel(level);
	}

	public BackPurviewDao getBackPurviewDao() {
		return backPurviewDao;
	}
	@Autowired
	public void setBackPurviewDao(BackPurviewDao backPurviewDao) {
		this.backPurviewDao = backPurviewDao;
	}

	public BackPurview getObject(Serializable id) {
		return backPurviewDao.getObject(BackPurview.class, id);
	}

	public List<BackPurview> getAllObjects() {
		return null;
	}

	public void removeObject(BackPurview obj) {
		backPurviewDao.removeObject(BackPurview.class, obj.getId());
	}
	
	public List<BackPurview> listAllPurviewForSysAdmin() {
		return backPurviewDao.listAllPurviewForSysAdmin();
	}
	

	public BackPurview saveObject(BackPurview obj) {
		return null;
	}

	public BackPurview updateObject(BackPurview obj) {
		return null;
	}







	




	
	
	


}

